package com.farm.authority.action;

import com.farm.authority.domain.Action;
import com.farm.authority.domain.Actiontree;
import com.farm.authority.service.ActionServiceInter;

import com.farm.web.WebSupport;
import com.farm.web.easyui.EasyUiTreeNode;
import com.farm.web.easyui.EasyUiUtils;

import java.util.List;
import java.util.Map;
import com.farm.core.page.CommitType;
import com.farm.core.page.PageSet;
import com.farm.core.page.PageType;
import com.farm.core.sql.query.DataQuery;
import com.farm.core.sql.result.DataResult;
import com.farm.core.sql.query.DBRule;
import com.farm.web.spring.BeanFactory;

/* *
 *功能：构造权限控制层
 *详细：
 *
 *版本：v0.1
 *作者：Farm代码工程自动生成
 *日期：20141122211253
 *说明：
 */
public class ActiontreeAction extends WebSupport {
	private Map<String, Object> jsonResult;// 结果集合
	private List<EasyUiTreeNode> treeNodes;// 异步树的封装
	private DataQuery query;// 条件查询
	private Actiontree entity;// 实体封装
	private Actiontree parent;// 实体封装
	private PageSet pageset;// 请求状态
	private String ids;// 主键集合
	private String id;
	private Action treeAction;
	private String domain;

	/**
	 * 查询结果集合
	 * 
	 * @return
	 */
	public String queryall() {
		try {
			query = EasyUiUtils.formatGridQuery(getRequest(), query);
			if (query.getQueryRule().size() == 0) {
				query.addRule(new DBRule("PARENTID", "NONE", "="));
			}
			DataResult result = actionIMP.createActiontreeSimpleQuery(query)
					.search();
			result.runDictionary("1:分类,2:菜单,3:权限", "TYPE");
			result.runDictionary("1:可用,0:禁用", "STATE");
			jsonResult = EasyUiUtils.formatGridData(result);
		} catch (Exception e) {
			throw new RuntimeException(e);
		}
		return SUCCESS;
	}

	/**
	 * 提交修改数据
	 * 
	 * @return
	 */
	public String editSubmit() {
		try {
			entity = actionIMP.editActiontreeEntity(entity, getCurrentUser(),
					treeAction.getAuthkey());
			pageset = new PageSet(PageType.UPDATE, CommitType.TRUE);
		} catch (Exception e) {
			pageset = PageSet.initPageSet(pageset, PageType.UPDATE,
					CommitType.FALSE, e);
		}
		return SUCCESS;
	}

	/**
	 * 提交新增数据
	 * 
	 * @return
	 */
	public String addSubmit() {
		try {
			entity = actionIMP.insertActiontreeEntity(entity, getCurrentUser(),
					treeAction.getAuthkey());
			pageset = new PageSet(PageType.ADD, CommitType.TRUE);
		} catch (Exception e) {
			pageset = PageSet.initPageSet(pageset, PageType.ADD,
					CommitType.FALSE, e);
		}
		return SUCCESS;
	}

	/**
	 * 删除数据
	 * 
	 * @return
	 */
	public String delSubmit() {
		// TODO 自动生成代码,修改后请去除本注释
		try {
			for (String id : parseIds(ids)) {
				actionIMP.deleteActiontreeEntity(id, getCurrentUser());
			}
			pageset = new PageSet(PageType.ADD, CommitType.TRUE);
		} catch (Exception e) {
			pageset = PageSet.initPageSet(pageset, PageType.DEL,
					CommitType.FALSE, e);
		}
		return SUCCESS;
	}

	/**
	 * 跳转
	 * 
	 * @return
	 */
	public String forSend() {
		// TODO 自动生成代码,修改后请去除本注释
		return SUCCESS;
	}

	/**
	 * 显示详细信息（修改或浏览时）
	 * 
	 * @return
	 */
	public String view() {
		// TODO 自动生成代码,修改后请去除本注释
		try {
			switch (pageset.getPageType()) {
			case (1): {// 新增
				break;
			}
			case (0): {// 展示
				entity = actionIMP.getActiontreeEntity(ids);
				parent = new Actiontree();
				parent.setId(entity.getParentid());
				break;
			}
			case (2): {// 修改
				entity = actionIMP.getActiontreeEntity(ids);
				parent = new Actiontree();
				parent.setId(entity.getParentid());
				break;
			}
			default:
				break;
			}
			if (parent != null) {
				parent = actionIMP.getActiontreeEntity(parent.getId());
			}
		} catch (Exception e) {
			pageset = PageSet.initPageSet(pageset, PageType.OTHER,
					CommitType.FALSE, e);
		}
		return SUCCESS;
	}

	/**
	 * 移动节点
	 * 
	 * @return
	 */
	public String moveTreeNodeSubmit() {
		try {
			actionIMP.moveActionTreeNode(ids, id);
			pageset = new PageSet(PageType.UPDATE, CommitType.TRUE);
		} catch (Exception e) {
			pageset = PageSet.initPageSet(pageset, PageType.UPDATE,
					CommitType.FALSE, e);
		}
		return SUCCESS;
	}

	public String loadTreeNode() {
		treeNodes = actionIMP.getSyncTree(id, domain);
		return SUCCESS;
	}

	/**
	 * 加载树节点根据用户权限
	 * 
	 * @return
	 */
	public String loadTreeNodeForCurrentUser() {
		List<Map<String, Object>> list1 = EasyUiTreeNode
				.queryTreeNodeOne(
						id,
						"SORT",
						"(SELECT c.NAME    AS NAME, SORT, c.ID      AS ID,  PARENTID, ICON, b.AUTHKEY     AS URL, c.PARAMS  AS PARAM, domain FROM alone_auth_actiontree c LEFT JOIN alone_auth_action b ON c.ACTIONID = b.ID)",
						"ID", "PARENTID", "NAME", "ICON",
						"and a.DOMAIN='" + domain + "'", "URL,PARAM")
				.getResultList();
		
		
		List<Map<String, Object>> list2 = EasyUiTreeNode.queryTreeNodeTow(id,
				"SORT", "alone_auth_actiontree", "ID", "PARENTID", "NAME", "ICON",
				"and a.DOMAIN='" + domain + "'").getResultList();
		
		
		
		treeNodes = EasyUiTreeNode.formatAsyncAjaxTreeForMenuTree(list1, list2,
				"PARENTID", "ID", "NAME", "ICON", "URL", "PARAM",
				getCurrentUserMenus());
		return SUCCESS;
	}

	private final static ActionServiceInter actionIMP = (ActionServiceInter) BeanFactory
			.getBean("farm_authority_action_ProxyId");

	// ----------------------------------------------------------------------------------
	public DataQuery getQuery() {
		return query;
	}

	public void setQuery(DataQuery query) {
		this.query = query;
	}

	public Actiontree getEntity() {
		return entity;
	}

	public void setEntity(Actiontree entity) {
		this.entity = entity;
	}

	public PageSet getPageset() {
		return pageset;
	}

	public void setPageset(PageSet pageset) {
		this.pageset = pageset;
	}

	public String getIds() {
		return ids;
	}

	public void setIds(String ids) {
		this.ids = ids;
	}

	public Map<String, Object> getJsonResult() {
		return jsonResult;
	}

	public void setJsonResult(Map<String, Object> jsonResult) {
		this.jsonResult = jsonResult;
	}

	public Actiontree getParent() {
		return parent;
	}

	public void setParent(Actiontree parent) {
		this.parent = parent;
	}

	public String getId() {
		return id;
	}

	public void setId(String id) {
		this.id = id;
	}

	public List<EasyUiTreeNode> getTreeNodes() {
		return treeNodes;
	}

	public void setTreeNodes(List<EasyUiTreeNode> treeNodes) {
		this.treeNodes = treeNodes;
	}

	public String getDomain() {
		return domain;
	}

	public void setDomain(String domain) {
		this.domain = domain;
	}

	public Action getTreeAction() {
		return treeAction;
	}

	public void setTreeAction(Action treeAction) {
		this.treeAction = treeAction;
	}

	// private static final Logger log =
	// Logger.getLogger(ActiontreeAction.class);
	private static final long serialVersionUID = 1L;
}
